package weblab.tutor;

import phonegap.ex.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class PhoneGapExActivity extends Activity{
    final Context myApp = this; 

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        WebView wv = (WebView) findViewById(R.id.webView);
        wv.loadUrl("file:///android_asset/www/index.html");
        //super.loadUrl("file:///android_asset/www/index.html");
        
      
    
    class JavascriptInterface   {  
        @SuppressWarnings("unused")  
        public void getUsername(String html)  {  
            /*new AlertDialog.Builder(myApp)  
                .setTitle("HTML")  
                .setMessage(html)  
                .setPositiveButton(android.R.string.ok, null)  
            .setCancelable(false)  
            .create()  
            .show();*/
        	System.out.println(html);
        	String username=html;
        }  
        @SuppressWarnings("unused")
		public void getPassword(String html)  {  
            /*new AlertDialog.Builder(myApp)  
                .setTitle("HTML")  
                .setMessage(html)  
                .setPositiveButton(android.R.string.ok, null)  
            .setCancelable(false)  
            .create()  
            .show();*/
        	System.out.println(html);
        	String password=html;
        }  
    }  
      
    final WebView browser = (WebView)findViewById(R.id.webView);  
    /* JavaScript must be enabled if you want it to work, obviously */   
    browser.getSettings().setJavaScriptEnabled(true);  
      
    /* Register a new JavaScript interface called HTMLOUT */  
    browser.addJavascriptInterface(new JavascriptInterface(), "HTMLOUT");  
      
    /* WebViewClient must be set BEFORE calling loadUrl! */  
    browser.setWebViewClient(new WebViewClient() {  
        @Override  
        public void onPageFinished(WebView view, String url){  
            /* This call inject JavaScript into the page which just finished loading. */  
            browser.loadUrl("javascript:window.HTMLOUT.getUsername(document.getElementsByTagName('input')[0].value);");
            browser.loadUrl("javascript:window.HTMLOUT.getPassword(document.getElementsByTagName('input')[1].value);");

        }  
    });  
      
    /* load a web page */  
    browser.loadUrl("file:///android_asset/www/index.html"); 
    }
}